package com.lanhai.zhonglai.dispatch;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.lanhai.zhonglai.MsgCore;
import com.lanhai.zhonglai.beans.AgvBean;
import com.lanhai.zhonglai.beans.AgvMountBean;
import com.lanhai.zhonglai.beans.DotBean;
import com.lanhai.zhonglai.beans.StationBean;
import com.lanhai.zhonglai.beans.StationMountBean;
import com.lanhai.zhonglai.enums.AgvCurrentTransEnum;
import com.lanhai.zhonglai.enums.AgvMountTypeEnum;
import com.lanhai.zhonglai.enums.TransDirectionEnum;
import com.lanhai.zhonglai.interfaces.IAgv;
import com.lanhai.zhonglai.interfaces.IAgvEvent;
import com.lanhai.zhonglai.interfaces.ICmdLog;
import com.lanhai.zhonglai.interfaces.IControlArea;
import com.lanhai.zhonglai.interfaces.IDot;
import com.lanhai.zhonglai.interfaces.IDotEvent;
import com.lanhai.zhonglai.interfaces.IRoad;
import com.lanhai.zhonglai.interfaces.ISendData;
import com.lanhai.zhonglai.interfaces.IStation;
import com.lanhai.zhonglai.interfaces.IStationMount;
import com.lanhai.zhonglai.interfaces.IStationProtocol;
import com.lanhai.zhonglai.third.StationFactory;
//弃用
@Component
public class JudgementALD_S2 {

	@Autowired
	IStation iStation;

	@Autowired
	IStationMount iStationMount;

	@Autowired
	IAgv iAgv;

	@Autowired
	IDot iDot;

	@Autowired
	ISendData sendTools;

	@Autowired
	IAgvEvent iAgvEvent;

	@Autowired
	IDotEvent iDotEvent;

	@Autowired
	IRoad iRoad;

	@Autowired
	ICmdLog iCmdLog;

	@Autowired
	IControlArea iControlArea;

	@Autowired
	AgvCmdMap agvCmdMap;

	@Autowired
	StationLeaveCheck stationCheck;

	/**
	 * 超过10个花篮的
	 * @param agv
	 * @param station
	 * @param dot
	 * @param agvMount
	 * @param dmount
	 */
	public void judgement_twice(AgvBean agv, StationBean station, DotBean dot, AgvMountBean agvMount,StationMountBean dmount) {
		IStationProtocol stationEvent = StationFactory.getStation(station.getCmdSign());

		if (StationCmdMap.canLeave(station.getDotno())&&agv.isFinishTrans()==false) {
			 if(agvMount.getMountType()==AgvMountTypeEnum.UpFullAndDownEmpty ) {  
				byte[] cmd = stationEvent.agvLeave(station.getLine(), station.getFillType());
				sendTools.sendStationData(station.getIp(), station.getPort(), station.getLine(), "AGV离开", cmd,
						station.getStationname());
				
				
				
				if(iControlArea.isLockedForDot(dot.getDotno(),agv.getAgvId())) { 
					//前方交管区被管了
					return;
				}
				stationCheck.check(dot, agv, station, stationEvent, agvMount);
				return;
			 }else {
				 //其它状态，一般来说，需要AGV出料
				 //用于最后一段的“假离开”
				 StationCmdMap.clearState(dot.getDotno());
				 
				 //更新AGV
					agv.setFakeLeave(false); 
				 iAgv.saveCache(agv);
				 
				 //给机台发送AGV已经离开 ,直到对方不发“允许离开”
					byte[] goout = stationEvent.agvLeave(station.getLine(), station.getFillType());
					sendTools.sendStationData(station.getIp(), station.getPort(), station.getLine(), "AGV假离开", goout,
							station.getStationname());
					return;
				 
			 }
		} 
		 // ********************* 处理转动 ********************** 
		if (StationCmdMap.canReceive(station.getDotno()) && agv.getCurrentTrans()!=AgvCurrentTransEnum.None) { 
			// 传输过程中，直接过滤 
			if (agv.isTransing()==false) {
				 agv.setTransing(true);
				 iAgv.saveCache(agv);
				switch (agv.getCurrentTrans()) {
				 //只有出料的动作
				case DownIn:
					iAgvEvent.transDown(agv, true);
					break;
				case DownOut:
					iAgvEvent.transDown(agv, false);
					break;
				case UpIn:
					iAgvEvent.transUp(agv, true);
					break;
				case UpOut:
					iAgvEvent.transUp(agv, false);
					break;
				case UpInAndDownOut:
					iAgvEvent.transUpInAndDownOut(agv);
					break;
				case UpOutAndDownIn:
					iAgvEvent.transUpOutAndDownIn(agv);
					break;
				default:
					MsgCore.sendMsg(agv.getAgvId(), "未知的传输模式");
					break;
				}
			}
			return;
		} 
		// ****************  处理传动 end  **************** 
		
		
		// **************** 处理传输状态  ********
		  if(agvMount.getMountType()==AgvMountTypeEnum.UpFullAndDownEmpty && agv.isFinishTrans()==false) {
			 //满足离开的装料状态
			 agv.setFinishTrans(true);
			 iAgv.saveCache(agv);
		 }
		 
		if(agv.isFinishTrans()) {
			//申请离开
			if (agv.getLastSaveDot() != dot.getDotno()) {
				 iCmdLog.saveTransLog(agv.getAgvId(), dot.getDotno(), station, TransDirectionEnum.Both); 
				agv.setLastSaveDot(dot.getDotno());
				iAgv.saveCache(agv);
			}
			  
			byte[] goout = stationEvent.applyLeave(station.getLine(), station.getFillType()); 
			sendTools.sendStationData(station.getIp(), station.getPort(), station.getLine(), "AGV申请离开", goout,
					station.getStationname());
			return;
		}
		
		// **************** 处理传输状态 end ********
		
		// 机台允许传输，开始申请皮带转动（申请传输）
		if (StationCmdMap.canTrans(station.getDotno())  ) {
			
			if(agvMount.getMountType()==AgvMountTypeEnum.AllFull) {
				//全满，AGV下出
				if(dmount.isAllowIn()) {
					agv.setCurrentTrans(AgvCurrentTransEnum.DownOut); 
					agv.setBothTrans(false);
					agv.setApplyTransType(TransDirectionEnum.OnlyIn);
					iAgv.saveCache(agv);
					MsgCore.sendMsg(agv.getAgvId(), "AGV发起申请传输-传入");
					byte[] cmd = stationEvent.applyRecieve(station.getLine(), TransDirectionEnum.OnlyIn);
					sendTools.sendStationData(station.getIp(), station.getPort(), station.getLine(), "AGV申请传入", cmd,
							station.getStationname()); 
				}else {
					//料不够申请
					MsgCore.sendMsg(agv.getAgvId(), String.format("待传输花篮数量，上料%d,出料%d", dmount.getInNum(),dmount.getOutNum()) );
					 
				}
				return;
			}else if(agvMount.getMountType()==AgvMountTypeEnum.UpEmptyAndDownFull) {
				//上空下满，因为本类是处理超过10个的，这种情况只能上层收货
				if(dmount.isAllowOut()) {
					//AGV上层收货
					agv.setCurrentTrans(AgvCurrentTransEnum.UpIn); 
					agv.setBothTrans(false);
					agv.setApplyTransType(TransDirectionEnum.OnlyOut);
					iAgv.saveCache(agv);
					MsgCore.sendMsg(agv.getAgvId(), "AGV发起申请传输-传入");
					byte[] cmd = stationEvent.applyRecieve(station.getLine(), TransDirectionEnum.OnlyOut);
					sendTools.sendStationData(station.getIp(), station.getPort(), station.getLine(), "AGV申请传入", cmd,
							station.getStationname());  
				}else {
					//料不够申请
					MsgCore.sendMsg(agv.getAgvId(), String.format("待传输花篮数量，上料%d,出料%d", dmount.getInNum(),dmount.getOutNum()) );
					 
				}
				return;
			}else {
				//其它状态
				
				return;
			}
			  
		} 
 
		// ********  机台允许传输  end ******** 
		if(agvMount.getMountType()==AgvMountTypeEnum.AllFull) {
			//先发AGV离开，再发AGV就位
			if(dmount.isAllowIn()) {
				byte[] cmd = stationEvent.agvArrived(station.getLine(), TransDirectionEnum.Both);
				sendTools.sendStationData(station.getIp(), station.getPort(), station.getLine(), "AGV就位", cmd,
						station.getStationname());
			}else {
				if(agv.isFakeLeave()) {
					//假离开了，前面已经发过，此处不发
				}else {
					//申请假离开，申请几次就可以了
					byte[] cmd = stationEvent.applyLeave(station.getLine(), TransDirectionEnum.Both);
					sendTools.sendStationData(station.getIp(), station.getPort(), station.getLine(), "AGV申请离开", cmd,
							station.getStationname());
				}
				
			}  
		}else {
			//其它状态，可以收货，不会有空车出现
			if(dmount.isAllowOut()) {
				byte[] cmd = stationEvent.agvArrived(station.getLine(), TransDirectionEnum.Both);
				sendTools.sendStationData(station.getIp(), station.getPort(), station.getLine(), "AGV就位", cmd,
						station.getStationname());
			} else {
				//量不够，不用AGV就位
				MsgCore.sendMsg(agv.getAgvId(), String.format("待传输花篮数量，上料%d,出料%d", dmount.getInNum(),dmount.getOutNum()) );
				
			}
		} 
	} 
}
